<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm @register="register" @reset="handleReset" @submit="handleSubmit">
      <template #areaListSelect="{ model, field }">
        <n-select
          v-model:value="model[field]"
          :options="optionsRef"
          clearable
          filterable
          placeholder="选择区服"
        />
      </template>
    </BasicForm>

    <BasicTable
      ref="actionRef"
      :actionColumn="actionColumn"
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row) => row.id"
      :scroll-x="2048"
      @update:checked-row-keys="onCheckedRow"
    >
      <template #tableTitle>
        <span><b>汇总</b></span> <b>:</b>
        <span style="color: red; margin-left: 10px"
          ><b>{{ totalAmount }}</b></span
        >
      </template>
      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>
  </n-card>
</template>

<script lang="ts" setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import { useDialog, useMessage } from 'naive-ui';
  import { BasicTable } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { columns } from './columns';
  import { useRouter } from 'vue-router';
  import { getRechargeRecord } from '@/api/player/rechargeRecord';
  import { formatToDate } from '@/utils/dateUtil';
  import { getAllArea } from '@/api/game/game';

  const schemas: FormSchema[] = [
    {
      field: 'userId',
      component: 'NInput',
      label: '玩家ID',
      componentProps: {
        placeholder: '玩家ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'roleId',
      component: 'NInput',
      label: '角色ID',
      componentProps: {
        placeholder: '角色ID',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'money',
      component: 'NInput',
      label: '充值总额',
      componentProps: {
        placeholder: '例：=99、 >99、 <99、 10-99',
        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'serverId',
      label: '区服',
      slot: 'areaListSelect',
    },
    {
      field: 'createTime',
      component: 'NDatePicker',
      label: '注册时间',
      componentProps: {
        placeholder: '注册时间',
        type: 'daterange',

        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'payTime',
      component: 'NDatePicker',
      label: '充值时间',
      componentProps: {
        placeholder: '充值时间',
        type: 'daterange',

        onInput: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const router = useRouter();
  const formRef: any = ref(null);
  const message = useMessage();
  const dialog = useDialog();
  const actionRef = ref();
  const optionsRef = ref([]);
  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    userId: 0,
    serverId: 0,
    money: '',
    createTime: [0, 0],
    payTime: [0, 0],
  });

  const params = ref({
    userId: 0,
    serverId: 0,
    money: '',
    createTime: [0, 0],
    payTime: [0, 0],
  });

  // const actionColumn = reactive({
  //   width: 100,
  //   title: '操作',
  //   key: 'action',
  //   fixed: 'right',
  //   render(record) {
  //     return h(TableAction as any, {
  //       style: 'button',
  //       actions: [
  //         {
  //           label: '充值流水',
  //           onClick: handleDetail.bind(null, record),
  //           ifShow: () => {
  //             return true;
  //           },
  //         },
  //       ],
  //       select: (key) => {
  //         message.info(`您点击了，${key} 按钮`);
  //       },
  //     });
  //   },
  // });

  const [register, {}] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:5 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  function addTable() {
    showModal.value = true;
  }

  var totalAmount = 0;
  const loadDataTable = async (res) => {
    if (!formParams.userId) {
      formParams.userId = 0;
    } else {
      formParams.userId = parseInt(formParams.userId);
    }
    if (!formParams.serverId) {
      formParams.serverId = 0;
    } else {
      formParams.serverId = parseInt(formParams.serverId);
    }
    const param = { ...formParams, ...res };
    console.log(param);
    delete param.makeDate;
    const data = await getRechargeRecord(param);
    totalAmount = data.totalAmount;
    return data;
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function handleDetail(record: Recordable) {
    // console.log('点击了编辑', record);
    // router.push({name: 'rechargeFlow', params: {id: record.userId}});
  }

  function handleSubmit(values: Recordable) {
    console.log('submit', values);
    let tempParams = unref(formParams);
    tempParams = Object.assign(tempParams, values);
    if (values.makeDate) {
      let [startTime, endTime]: [number, number] = values.makeDate;
      let startTimeS = formatToDate(new Date(startTime));
      let endTimeS = formatToDate(new Date(endTime));
      tempParams = Object.assign(tempParams, { startTime: startTimeS, endTime: endTimeS });
    }
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }

  onMounted(async () => {
    let areaVal = await getAllArea();
    optionsRef.value = areaVal.map((value) => {
      return {
        label: value.serverName,
        value: value.serverId,
      };
    });
  });
</script>

<style lang="less" scoped></style>
